<?php

// 接受参数
$dep_id = isset($_GET['dep_id']) ? $_GET['dep_id'] : null;

if ($dep_id){

    require_once('./dbfunc.php');
    require_once('./dbparam.php');

    header('Content-Type: json');

    // 连接数据库
    $conn = conn_mysql();

    // 响应体
    $response = [];

    /* 查询班级 */
    $sql = "
    select cl_id, cl_name 
    from huangcz_class, huangcz_major
    where cl_major = ma_id
    and ma_dep = ?;
    ";

    // 结果参数
    $cl_id = 0;
    $cl_name = '';

    $stmt = $conn->prepare($sql);
    $stmt->bind_param('i', $dep_id);
    $stmt->bind_result($cl_id, $cl_name);
    $stmt->execute();
    $stmt->store_result();

    
    $cls = [];
    while ($stmt->fetch()){
        // 存储学院所有的班级
        $cls[] = [
            'cl_id' => $cl_id,
            'cl_name' => $cl_name
        ];
    }
    $stmt->free_result();

    /* 查询课程 */
    $sql = "
            select co_id, co_name
                from huangcz_course, huangcz_major
                where co_major = ma_id
                and ma_dep = ?;
    ";

    $stmt = $conn->prepare($sql);
    $stmt->bind_param('i', $dep_id);

    // 结果参数
    $co_id = 0;
    $co_name = '';

    $stmt->bind_result($co_id, $co_name);
    $stmt->execute();
    $stmt->store_result();

    $courses = [];
    while($stmt->fetch()){
        // 存储学院所有的课程
        $courses[] = [
            'co_id' => $co_id,
            'co_name' => $co_name
        ];
    }

    $stmt->free_result();

    /* 查询教师 */
    $sql = "select te_id, te_name 
                from huangcz_te
                where te_dep = ?;";
    
    $stmt = $conn->prepare($sql);
    
    $stmt->bind_param('i', $dep_id);
    
    // 结果参数
    $te_name = '';
    $te_id = 0;
    
    $stmt->bind_result($te_id, $te_name);
    $stmt->execute();
    $stmt->store_result();
    
    // 响应内容
    $response = [];

    $tes = [];
    
    while ($stmt->fetch()){
        // 存储学院所有的教师
        $tes[] = [
            'te_id' => $te_id,
            'te_name' => $te_name
        ];
    }
    
    $stmt->free_result();

    $response = [
        'cls' => $cls,
        'courses' => $courses,
        'tes' => $tes
    ];

    exit(json_encode($response, JSON_UNESCAPED_UNICODE));

}else{
    exit(json_encode([]));
}